import { init_index_api, type InitIndex } from "@/api/guider_home";
import { Storage } from "./database";

const userDataKey = "userData";
const envKey = "env";

export class AppData {
  private static _userData: AppDataUserData;

  static get userData() {
    if (!AppData._userData) {
      AppData._userData =
        Storage.getSync<AppDataUserData>(userDataKey) || userData;
    }
    return AppData._userData;
  }

  static set userData(userData) {
    AppData._userData = userData;
    Storage.setSync(userDataKey, userData);
  }

  static deleteUserData() {
    AppData._userData = userData;
    Storage.deleteSync(userDataKey);
  }

  private static _domain: AppDataDomain;

  static get domain() {
    if (!AppData._domain) {
      AppData._domain = "https://ss.sanmaoyou.com";
    }
    return AppData._domain;
  }

  static set domain(domain) {
    AppData._domain = domain;
  }

  private static _env: AppDataEnv;

  static get env() {
    if (!AppData._env) {
      AppData._env = uni.getStorageSync(envKey) || "prod"; // 默认环境
    }
    return AppData._env;
  }

  static set env(env) {
    AppData._env = env;
    uni.setStorageSync(envKey, env);
  }

  private static _rectData: AppDataRectData;

  static get rectData() {
    if (!AppData._rectData) {
      AppData._rectData = rectData;
    }
    return AppData._rectData;
  }

  static set rectData(rectData) {
    AppData._rectData = rectData;
  }

  private static _bundleData: AppDataBundleData;

  static get bundleData() {
    if (!AppData._bundleData) {
      AppData._bundleData = bundleData;
    }
    return AppData._bundleData;
  }

  static set bundleData(bundleData) {
    AppData._bundleData = bundleData;
  }

  private static _initConfig: InitIndex;

  static initConfig(callback: (config: InitIndex) => void) {
    const _initConfig = AppData._initConfig;
    _initConfig
      ? callback(_initConfig)
      : init_index_api().then(config => {
          callback(config);
          AppData._initConfig = config;
        });
  }

  private static _positionData: AppDataPositionData;

  static get positionData() {
    if (!AppData._positionData) {
      AppData._positionData = positionData;
    }
    return AppData._positionData;
  }

  static set positionData(positionData) {
    AppData._positionData = positionData;
  }

  private static _systemType: AppDataSystemType;

  static get systemType() {
    if (!AppData._systemType) {
      AppData._systemType = "android";
    }
    return AppData._systemType;
  }

  static set systemType(systemType) {
    AppData._systemType = systemType;
  }
}

const userData: AppDataUserData = {
  uid: 0,
  account: "",
  nick_name: "",
  third_img: "",
  telephone: "",
  gender: 0,
  openid: "",
  login_token: "",
  tag_id: 113,
  is_guide_certification: 0,
  status: -1,
  is_vip_member: 0,
};
const rectData: AppDataRectData = {
  windowWidth: 0,
  statusBarHeight: 0,
  menuButtonWidth: 0,
  menuButtonHeight: 0,
  menuButtonTop: 0,
  menuButtonRight: 0,
  navBarHeight: 0,
  topBarHeight: 0,
};
const bundleData: AppDataBundleData = {
  bundleName: "",
  versionName: "",
  tagId: "",
};
const positionData: AppDataPositionData = {
  cityId: "",
  cityName: "",
};

export interface AppDataUserData {
  uid: number;
  account: string;
  nick_name: string;
  third_img: string;
  telephone: string;
  gender: number;
  openid: string;
  login_token: string;
  tag_id: number;
  is_guide_certification: number; // 认证导游
  status: number; // 导游基础认证审核状态  -1：未认证；0：未审核；1：已认证；2：审核不通过
  is_vip_member: number; // 是否vip
}

type AppDataDomain =
  | "https://ss.sanmaoyou.com"
  | "https://sstest.sanmaoyou.com";
type AppDataEnv = "prod" | "dev";
type AppDataSystemType = "ios" | "android";

interface AppDataRectData {
  windowWidth: number; // 可视窗口宽度
  statusBarHeight: number; // 状态栏高度
  menuButtonWidth: number; //胶囊按钮宽度
  menuButtonHeight: number; //胶囊按钮高度
  menuButtonTop: number; // 胶囊按钮上边距
  menuButtonRight: number; // 胶囊按钮右边距
  navBarHeight: number; // 导航栏高度
  topBarHeight: number; // 顶部高度
}

interface AppDataBundleData {
  bundleName: string;
  versionName: string;
  tagId: string;
}

interface AppDataPositionData {
  cityId: string;
  cityName: string;
}
